package com.app.teaware.bean;

import android.text.TextUtils;

import com.app.teaware.greendao.GreenDaoUtils;
import com.app.teaware.util.UnitUtil;

import java.util.ArrayList;
import java.util.List;

import my.MathUtils;

public class GoodsSubmitData extends BasisBean {

    /**
     * {
     *   "workm": {
     *     "aid": "string",
     *     "bfs": "string",
     *     "cid": "string",
     *     "cna": "string",
     *     "mdt": "yyyy-MM-dd",
     *     "mht": "string",
     *     "mid": "string",
     *     "mna": "string",
     *     "mno": "string",
     *     "mty": "string",
     *     "nch": "string",
     *     "tys": "string",
     *     "udt": "yyyy-MM-dd",
     *     "una": "string",
     *     "uno": "string",
     *     "upm": "string",
     *     "wch": "string",
     *     "wno": "string"
     *   },
     *   "works": [
     *     {
     *       "aid": "string",
     *       "bfs": "string",
     *       "cid": "string",
     *       "gid": "string",
     *       "gna": "string",
     *       "gno": "string",
     *       "gnu": 0,
     *       "gsp": "string",
     *       "gun": "string",
     *       "gwt": 0,
     *       "mdt": "yyyy-MM-dd",
     *       "mid": "string",
     *       "rfid": "string",
     *       "sid": "string",
     *       "wms": "string",
     *       "wno": "string"
     *     }
     *   ]
     * }
     */
    private static final long serialVersionUID = 1L;


    private GoodsWorkm workm;
    private List<GoodsWorks> works;

    public GoodsWorkm getWorkm() {
        return workm;
    }

    public void setWorkm(GoodsWorkm workm) {
        this.workm = workm;
    }

    public List<GoodsWorks> getWorks() {
        if(works == null){
            works = new ArrayList<>();
        }
        return works;
    }

    public void setWorks(List<GoodsWorks> works) {
        this.works = works;
    }

    public void setWorksLoc(String loc){
        for (GoodsWorks goodsWorks : works){
            goodsWorks.setLoc(loc);
        }
    }


    public String getWeight(){
        String weight = "0";
        List<GoodsWorks> worksList =  getWorks();
        for (GoodsWorks work : worksList){
            double a = UnitUtil.getRatio(work.getGun(),"kg");
            boolean isSameUtil = a == 0;
            if(isSameUtil){
                weight = MathUtils.add(weight,MathUtils.multiply(work.getGwt(),String.valueOf(a)));
            }else {
                weight = MathUtils.add(weight,work.getGwt());
            }

        }
        return weight;
    }

    public String getNum(){
        String num = "0";
        List<GoodsWorks> worksList =  getWorks();
        for (GoodsWorks work : worksList){
            if(!TextUtils.isEmpty(work.getGnu())) {
                num = MathUtils.add(num, work.getGnu());
            }
        }
        return num;
    }


    public String getAllBwt(){      //获取到所有重量
        String num = "0";
        List<GoodsWorks> worksList =  getWorks();
        if(worksList == null || worksList.size()<1){
            return num;
        }
        for (GoodsWorks work : worksList){
            if(!TextUtils.isEmpty(work.getGwt())){
                num = MathUtils.add(num,work.getGwt());
            }
        }
        return num;
    }

    public String getAllBwf(){      //获取到所有纸箱重量
        String num = "0";
        List<GoodsWorks> worksList =  getWorks();
        if(worksList == null || worksList.size()<1){
            return num;
        }
        for (GoodsWorks work : worksList){
            if(!TextUtils.isEmpty(work.getBwf())){
                num = MathUtils.add(num,work.getBwf());
            }
        }
        return num;
    }

    //获取误差值
    public String getMinValue(){
        String minValue = "";
        for(GoodsWorks goodsWork : works){
//            minValue = MathUtils.compareToGetMinValue(minValue, GreenDaoUtils.getGwtByGidFromGoodsTable(goodsWork.getGid()));
            minValue = MathUtils.compareToGetMinValue(minValue, goodsWork.getGwt());
        }
        return minValue;
    }

    public String getMaxValue(){
        String maxValue = "";
        for(GoodsWorks goodsWork : works){
//            minValue = MathUtils.compareToGetMinValue(minValue, GreenDaoUtils.getGwtByGidFromGoodsTable(goodsWork.getGid()));
            maxValue = MathUtils.compareToGetMaxValue(maxValue, goodsWork.getGwt());
        }
        return maxValue;
    }


    public static final int NOT_ENOUGH = 12;
    public static final int OUT = 13;
    public static final int CONTINUE = 14;
    public int toCheck(String toothWeight,String allWeight){
        int resultStaus =14;

        String wcValue = getMinValue();//最小误差值
//        String wcValue = MathUtils.div(getMaxValue(),"2");//误差值
        int contrast =  MathUtils.compareTo(toothWeight,allWeight);
        /**
         *对比值
         * 	contrast -1,表示bg1小于bg2；
         * //		a == 0,表示bg1等于bg2；
         * //		a == 1,表示bg1大于bg2；
         */
        switch (contrast){
            case -1: //总重量（领料重量） > 蓝牙的重量                       重量不足
                //余
                String remains = MathUtils.subtract(allWeight,toothWeight);
                int a =  MathUtils.compareTo(remains,wcValue);
                if(a != -1){
                    resultStaus = NOT_ENOUGH;
                }
                break;
            case 0:
                resultStaus = CONTINUE;
                break;
            case 1:  //总重量（领料重量） < 蓝牙的重量                       重量超重
                //余
                String remainsb = MathUtils.subtract(toothWeight,allWeight);
                int b =  MathUtils.compareTo(remainsb,wcValue);
                if(b != -1){
                    resultStaus = OUT;
                }
                break;
        }

        return resultStaus;
    }

}
